package com.vincent.kdp.ui.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.RelativeLayout;
import com.vincent.baseapp.base.BaseFragment;
import com.vincent.baseapp.util.SPUtil;
import com.vincent.baseapp.util.view.NoScrollerListView;
import com.vincent.kdp.Activity.GoodsDetailActivity;
import com.vincent.kdp.Activity.SelectSearchActivity;
import com.vincent.kdp.R;
import com.vincent.kdp.adapter.MyGlideImageLoader;
import com.vincent.kdp.bean.MyBanner;
import com.vincent.kdp.ui.adapter.BannerViewPagerAdapter;
import com.vincent.kdp.ui.adapter.HotCommoditiesAdapter;
import com.vincent.kdp.ui.bean.entity.HotCommodity;
import com.vincent.kdp.ui.commodity.CommodityListActivity;
import com.vincent.kdp.ui.fragment.contract.CommodityContract;
import com.vincent.kdp.ui.fragment.presenter.CommodityPresenter;
import com.vincent.kdp.util.BaseParam;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;
import com.youth.banner.listener.OnBannerListener;
import java.util.ArrayList;
import java.util.List;
import butterknife.Bind;
import butterknife.OnClick;

/**
 * 商品的fragment
 * Created by Administrator on 2016/5/28.
 */
public class CommodityFragment extends BaseFragment<CommodityContract.View, CommodityPresenter> implements CommodityContract.View {
    /*@Bind(R.id.autoScrollViewPager)
    AutoScrollViewPager mAutoScrollViewPager;*/
    @Bind(R.id.listView)
    NoScrollerListView mListView;

    @Bind(R.id.start_select)
    RelativeLayout selectClick;
    @Bind(R.id.commid_banner)
    Banner mNewBanner;

    //热门商品
    private HotCommoditiesAdapter mAdapter;
    //轮播图
    private BannerViewPagerAdapter mBannerAdapter;

    //存放图片链接集合
    private ArrayList<String> mList=new ArrayList<>();

    public CommodityFragment() {
        super();
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.fragment_commodity;
    }

    @Override
    protected void initViewData() {
        mListView.setFocusable(false);
        presenter.initView(null);
    }
   /* @Override
    public void initBannerViewPager(List<Banner> bannerList) {
        mBannerAdapter = new BannerViewPagerAdapter(getActivity(), bannerList);
        mAutoScrollViewPager.setAdapter(mBannerAdapter);
        mAutoScrollViewPager.setStopScrollWhenTouch(true);
        mAutoScrollViewPager.setCycle(true);
        mAutoScrollViewPager.setBorderAnimation(true);
        mAutoScrollViewPager.setAutoScrollDurationFactor(3);
        mAutoScrollViewPager.startAutoScroll();
    }*/

    /*@Override
    public void onResume() {
        super.onResume();
        mAutoScrollViewPager.startAutoScroll();
    }*/

    @Override
    public void initScrollViewPager(final List<MyBanner> bannerList) {
        for(int x=0;x<bannerList.size();x++){
            mList.add(bannerList.get(x).getItemImgUrl());
        }
        //设置banner样式
        mNewBanner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        mNewBanner.setImageLoader(new MyGlideImageLoader());

        //设置图片集合
        mNewBanner.setImages(mList);

        //设置banner动画效果
        mNewBanner.setBannerAnimation(Transformer.Default);

        //设置自动轮播，默认为true
        mNewBanner.isAutoPlay(true);
        //设置轮播时间
        mNewBanner.setDelayTime(3000);
        //设置指示器位置（当banner模式中有指示器时）

        mNewBanner.setIndicatorGravity(BannerConfig.CENTER);
        //banner设置方法全部调用完毕时最后调用
        mNewBanner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                SPUtil.put("seriesId",bannerList.get(position).getItemScroId());
                SPUtil.put("seriesName",bannerList.get(position).getItemName());
                mContext.startActivity(new Intent(mContext, CommodityListActivity.class));
            }
        });
        mNewBanner.start();
    }

    @Override
    public void initHotCommodities(List<HotCommodity> hotCommodityList) {
        mAdapter = new HotCommoditiesAdapter(getActivity(), hotCommodityList);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(onItemClickListener);
    }

    /*@Override
    public void startAutoScroll() {
        if (mAutoScrollViewPager != null) {
            mAutoScrollViewPager.startAutoScroll();
        }
    }*/

    @OnClick({R.id.start_select})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.start_select:
                Intent intent=new Intent(getActivity(), SelectSearchActivity.class);
                startActivity(intent);
                break;

        }
    }

    private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(getActivity(), GoodsDetailActivity.class);
            intent.putExtra("barCode", ((HotCommodity)mAdapter.getItem(position)).getBarCode());
            BaseParam.setCurrentBarCode(((HotCommodity) mAdapter.getItem(position)).getBarCode());
            startActivity(intent);
        }
    };

    @Override
    public CommodityPresenter initPresenter() {
        return new CommodityPresenter();
    }

    /*@Override
    public void onPause() {
        mAutoScrollViewPager.stopAutoScroll();
        super.onPause();
    }*/
}
